﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Net;
using System.Xml;
namespace DeserializeJson
{
    /*
    public class User
    {
        public User(string json)
        {
            JObject jObject = JObject.Parse(json);
            JToken jUser = jObject["user"];
            name = (string)jUser["name"];
            teamname = (string)jUser["teamname"];
            email = (string)jUser["email"];
            players = jUser["players"].ToArray();
        }

        public string name { get; set; }
        public string teamname { get; set; }
        public string email { get; set; }
        public Array players { get; set; }
    }
     * */

    public class DirectoryResponse
    {
        public string IP { get; set; }

        public string ServerIP { get; set; }

        public string ID { get; set; }

        public string ParentID { get; set; }

        public string Name { get; set; }

        public string type { get; set; }

        public string fileownername { get; set; }

        public string isshare { get; set; }

        public List<Entry> entry { get; set; }

        public ErrorResponse error { get; set; }

        public List<User> user { get; set; }
    }

    public class DirectoryFavoriteResponse
    {
        public List<Entry> entry { get; set; }
        public ErrorResponse error { get; set; }
    }

    public class DirectorySearchResponse
    {
        public List<Entry> entry { get; set; }
        public ErrorResponse error { get; set; }
    }

    public class Entry
    {
        public int ID { get; set; }
        public string Name { get; set; }

        public string Type { get; set; }

        public string Url { get; set; }

        // This is extend data member 
        // if not assign value that mean xml not populate this member
        public DateTime? ModifyDate { get; set; }
        public string Filesize { get; set; }

        public bool Isshare { get; set; }

        public string parent_id { get; set; }

        public string FavoriteFlag { get; set; }

        public ErrorResponse error { get; set; }

        public string owner { get; set; }

        public string share_to_user { get; set; }

        public List<User> user { get; set; }
    }

    public class ShareUserResponse
    {
        public string ID { get; set; }

        public string Name { get; set; }
        public List<User> Users { get; set; }

        public ErrorResponse error { get; set; }
    }

    public class User
    {
        public User()
        {

        }

        public User(string jsonStr)
        {
            JObject jObject = JObject.Parse(jsonStr);
            JToken jUser = jObject["user"];
            Name = (string)jUser["name"];
            email = (string)jUser["email"];
            ID = (string)jUser["id"];
            RegularFiles = (string)jUser["RegularFiles"];
            ShareFiles = (string)jUser["ShareFiles"];
            TotalSpace = (string)jUser["TotalSpace"];
            UseSpace = (string)jUser["UseSpace"];
            FreeSpace = (string)jUser["FreeSpace"];
            PercentUse = (string)jUser["PercetUse"];
            ImageLink = (string)jUser["ImageLink"];
            firstname = (string)jUser["firstname"];
            lastname = (string)jUser["lastname"];
            DiskQuota = (double)jUser["DiskQuota"];
            password = (string)jUser["password"];
            importflag = (string)jUser["importflag"];
           // error = (ErrorResponse)jUser["error"];
        }
        public string ID { get; set; }

        public string Name { get; set; }

        public string RegularFiles { get; set; }
        public string ShareFiles { get; set; }

        public string TotalSpace { get; set; }

        public string UseSpace { get; set; }

        public string FreeSpace { get; set; }

        public string PercentUse { get; set; }

        public string ImageLink { get; set; }
        public string email { get; set; }

        public string firstname { get; set; }

        public string lastname { get; set; }

        public double? DiskQuota { get; set; }

        public string password { get; set; }

        public string importflag { get; set; }

        public ErrorResponse error { get; set; }
    }

    public class PersonResponse
    {
        public List<User> users { get; set; }

        public ErrorResponse error { get; set; }
    }

    //Handle Login response sucess,Add favorite sucess
    public class ResponseStateInfo
    {
        public string Accesstoken { get; set; }

        public string Username { get; set; }

        public string Root_id { get; set; }

        public ErrorResponse error { get; set; }

        public Entry entry { get; set; }
    }


    public class ErrorResponse
    {
        public string Code { get; set; }
        public string Message { get; set; }
    }



    public class LogsInfoResponse
    {
        public string Logsid { get; set; }
        public string sDatetime { get; set; }
        public string Account { get; set; }
        public string Acction { get; set; }
        public string IP { get; set; }
        public string Server { get; set; }
        public string Status { get; set; }
        public string Error { get; set; }
    }


    class Program
    {
        static void Main(string[] args)
        {


            String URI = "http://localhost:7669/Service.svc/userInfo?username=suwet&access_token=b1d31480-cb18-45db-a0eb-bc1083e787bd";
            // ที่ เมธอด userInfo ต้องเลือก   BodyStyle = WebMessageBodyStyle.Bare เพื่อไม่ให้มันห่อหุ้ม response ที่จะส่งกลับ
            WebClient webClient = new WebClient();
            Stream stream = webClient.OpenRead(URI);
            StreamReader reader = new StreamReader(stream);
            String xmlStr = reader.ReadToEnd();

            Console.WriteLine(xmlStr);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlStr);
            

            /*
            string json = @"{""user"":{""name"":""asdf"",
             ""teamname"":""b"",""email"":""c"",""players"":[""1"",""2""]}}";
            */

            String jsonStr = JsonConvert.SerializeXmlNode(doc);
            Console.WriteLine(jsonStr);
            User us = new User(jsonStr);
         

            Console.ReadLine();
        }


    }
}
